package com.toolset.Activity.ToolItemActivity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.toolset.Activity.BaseActivity;
import com.toolset.R;
/*
Base64加密解码工具箱
 */
public class Base64Tool extends BaseActivity {

    private EditText base64EncodeEdit;
    private EditText base64DecodeEdit;

    private Button base64DecodeBtn;
    private Button base64EncodeBtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base64_tool);

        //初始化事件
        initEven();

        //--------------------加密-------------------------------------
        base64EncodeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text=base64EncodeEdit.getText().toString();
                if(text.length()==0)
                {
                    Toast.makeText(Base64Tool.this,"请输入内容！",Toast.LENGTH_SHORT).show();
                }
                else{
                  String result =   Base64.encodeToString(text.getBytes(),0);
                    base64DecodeEdit.setText(result);
                }
            }
        });

    //--------------------------解密------------------------------
        base64DecodeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text=base64DecodeEdit.getText().toString();//获取解密文本内容
                if (text.length()==0)
                {
                    Toast.makeText(Base64Tool.this,"请输入内容！",Toast.LENGTH_SHORT).show();
                }
                else{
                   String result = new String(Base64.decode(text.getBytes(),0));
                    base64EncodeEdit.setText(result);//在加密框显示
                }
            }
        });

    }

    //初始化事件
    private void initEven() {
        base64EncodeEdit = (EditText) findViewById(R.id.base64EncodeEdit);
        base64DecodeEdit= (EditText) findViewById(R.id.base64DecodeEdit);
        base64EncodeBtn= (Button) findViewById(R.id.base64EncodeBtn);
        base64DecodeBtn= (Button) findViewById(R.id.base64DecodeBtn);
    }

}
